package com.example.yiwei.crm.view.activity.healthActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ContextThemeWrapper;
import android.support.v7.widget.PopupMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetActivity;
import com.example.yiwei.crm.data.GetDock;
import com.example.yiwei.crm.data.GetDockData;
import com.example.yiwei.crm.data.GetVisit;
import com.example.yiwei.crm.entity.activityEntity;
import com.example.yiwei.crm.entity.dockingEntity;
import com.example.yiwei.crm.entity.scheduleEntity;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.view.frament.dockDetailedMenu.baseFragment;
import com.example.yiwei.crm.view.frament.dockDetailedMenu.mainFragment;
import com.example.yiwei.crm.view.frament.dockDetailedMenu.otherFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class DetailedHealthEducationActivity extends AppCompatActivity {
    private TextView txt_title;
    private ImageView img_back,img_update,img_more;
    int dock_id;
    boolean ButtonTag2 = false;
    boolean ButtonTag1 = false;
    boolean ButtonTag4 = false;
    FragmentManager manager;
    dockingEntity docking;
    List<dockingEntity> dockingEntityList;
    List<dockingEntity>  dockinglist;
    private Button btn_base,btn_main,btn_other;
    Fragment fragment_base,fragment_main,fragment_other;
    dockingEntity dockingEntity;
    private TextView txt_visit_number,txt_referral_amount,txt_clinical_amount,txt_operation_amount,
            txt_giving_count;
    private PopupMenu popupMenu;
    private Menu menu;
    List<scheduleEntity> scheduleEntityList;
    List<activityEntity> list;
    List<String> visit_list;
    private String visit_theme;
    private String[]themes;
    private String[]activity_themes;
    private int visit_theme_id;
    scheduleEntity schedule;
    List<String> activity_list;
    String activity_theme;
    int activity_id;
    activityEntity activityEntity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed_health_education);
        btn_base= (Button) findViewById(R.id.detailed_dock_frame_basemess);
        btn_main= (Button) findViewById(R.id.detailed_dock_frame_main);
        btn_other= (Button) findViewById(R.id.detailed_dock_frame_other);
        img_back= (ImageView) findViewById(R.id.detailed_dock_img_back);
        img_update= (ImageView) findViewById(R.id.detailed_dock_img_update);
        txt_title= (TextView) findViewById(R.id.detailed_dock_txt_name);
        txt_visit_number= (TextView) findViewById(R.id.detailed_dock_txt_visit_number);
        txt_clinical_amount= (TextView) findViewById(R.id.detailed_dock_txt_clinical_amount);
        txt_referral_amount= (TextView) findViewById(R.id.detailed_dock_txt_referral_amount);
        txt_operation_amount= (TextView) findViewById(R.id.detailed_dock_txt_operation_amount);
        txt_giving_count= (TextView) findViewById(R.id.detailed_dock_txt_giving_count);
        dockingEntity=GetDock.get();
        txt_title.setText(dockingEntity.getDocking_name());
        txt_visit_number.setText(dockingEntity.getVisit_number()+"");
        txt_clinical_amount.setText(dockingEntity.getClinical_amount()+"");
        txt_referral_amount.setText(dockingEntity.getReferral_amount()+"");
        txt_operation_amount.setText(dockingEntity.getOperation_amount()+"");
        txt_giving_count.setText(dockingEntity.getGiving_count()+"");
        dock_id=dockingEntity.getDocking_id();
        manager = getSupportFragmentManager();
        fragment_base=new baseFragment();
        fragment_main=new mainFragment();
        fragment_other=new otherFragment();
        OnClick(btn_base);
        popupMenu = new PopupMenu(this, findViewById(R.id.detailed_dock_img_more));
        menu = popupMenu.getMenu();
        final MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_dock, menu);
        //下拉框监听方法
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    //删除
                    case R.id.delete_dock:
                        AlertDialog.Builder builder;
                        builder = new AlertDialog.Builder(new ContextThemeWrapper(DetailedHealthEducationActivity.this, R.style.custom_dialog));
                        builder.setTitle("您确定要删除吗？");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        String result=NetWorkUtils.getCount(baseClass.delete_docking_message_url+dock_id,null);
                                        try {
                                            JSONObject object=new JSONObject(result);
                                            String boolen=object.getString("boolean");
                                            if ("success".equals(boolen)){
                                                dockDao dockDao=new dockDaoImpl();
                                                GetDockData.create();
                                                GetDockData.dockingEntityList=dockDao.queryAllDockingMessage(baseClass.query_docking_message_url,null);
                                                if (GetDockData.dockingEntityList!=null){
                                                    finish();
                                                }

                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }).start();
                            }
                        });
                        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });
                        builder.show();
                        break;
                    case R.id.visit_find:
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                           final dockDao dockDao=new dockDaoImpl();
                                scheduleEntityList=new ArrayList<scheduleEntity>();
                                scheduleEntityList=dockDao.queryVisitMessage(baseClass.find_visit_message_url+dock_id,null);
                                visit_list=new ArrayList<String>();
                                for (int i=0;i<scheduleEntityList.size();i++){
                                    visit_theme=scheduleEntityList.get(i).getSchedule_theme();
                                    visit_list.add(visit_theme);

                                }
                                themes=visit_list.toArray(new String[visit_list.size()]);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        AlertDialog.Builder builder;
                                        builder = new AlertDialog.Builder(new ContextThemeWrapper(DetailedHealthEducationActivity.this, R.style.custom_dialog));
                                        builder.setTitle("请选择日程主题");
                                        builder.setSingleChoiceItems(DetailedHealthEducationActivity.this.themes, -1, new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                visit_theme_id=scheduleEntityList.get(i).getVisit_id();
                                            }
                                        });
                                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                schedule=new scheduleEntity();
                                                GetVisit.create();
                                                for (int i1=0;i1<scheduleEntityList.size();i1++){
                                                    if (visit_theme_id==scheduleEntityList.get(i1).getVisit_id()){
                                                        schedule.setSchedule_theme(scheduleEntityList.get(i1).getSchedule_theme());
                                                        schedule.setVisit_id(scheduleEntityList.get(i1).getVisit_id());
                                                        schedule.setManager_name(scheduleEntityList.get(i1).getManager_name());
                                                        schedule.setTask_state(scheduleEntityList.get(i1).getTask_state());
                                                        schedule.setVisit_time(scheduleEntityList.get(i1).getVisit_time());
                                                        schedule.setVisit_gift(scheduleEntityList.get(i1).getVisit_gift());
                                                        schedule.setGift_value(scheduleEntityList.get(i1).getGift_value());
                                                        schedule.setCurrent_create_user_name(scheduleEntityList.get(i1).getCurrent_create_user_name());
                                                        schedule.setCreate_time(scheduleEntityList.get(i1).getCreate_time());
                                                        schedule.setVisit_id(scheduleEntityList.get(i1).getVisit_id());
                                                        schedule.setPriority_id(scheduleEntityList.get(i1).getPriority_id());
                                                        schedule.setTask_state_id(scheduleEntityList.get(i1).getTask_state_id());
                                                        schedule.setAdditional_remarks(scheduleEntityList.get(i1).getAdditional_remarks());

                                                    }

                                                }
                                                GetVisit.scheduleEntity=schedule;
                                                if (GetVisit.scheduleEntity!=null){
                                                    Intent intent=new Intent(DetailedHealthEducationActivity.this,detailedVisitActivity.class);
                                                    startActivity(intent);
                                                }

                                            }
                                        });
                                       builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                           @Override
                                           public void onClick(DialogInterface dialogInterface, int i) {

                                           }
                                       });
                                        builder.show();
                                    }
                                });
                            }
                        }).start();
                        break;
                    case R.id.visit_create:
                        Intent intent=new Intent(DetailedHealthEducationActivity.this,addVisitActivity.class);
                        intent.putExtra("docking_id",dock_id);
                        startActivity(intent);
                        break;
                    case R.id.activity_add:
                        Intent intent1=new Intent(DetailedHealthEducationActivity.this,addActivity.class);
                        intent1.putExtra("docking_id",dock_id);
                        startActivity(intent1);
                        break;
                    case R.id.activity_find:
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                list=new ArrayList<activityEntity>();
                                dockDao dockDao=new dockDaoImpl();
                                list=dockDao.queryActivityMessage(baseClass.find_activity_message_url+dock_id,null);
                                activity_list=new ArrayList<String>();
                                for (int i=0;i<list.size();i++){
                                    activity_theme=list.get(i).getActivity_theme();
                                    activity_list.add(activity_theme);
                                }
                                activity_themes=activity_list.toArray(new String[activity_list.size()]);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (activity_list!=null) {
                                            AlertDialog.Builder builder;
                                            builder = new AlertDialog.Builder(new ContextThemeWrapper(DetailedHealthEducationActivity.this, R.style.custom_dialog));
                                            builder.setTitle("请选择活动主题");
                                            builder.setSingleChoiceItems(DetailedHealthEducationActivity.this.activity_themes, -1, new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    activity_id = list.get(i).getActivity_id();
                                                }
                                            });
                                            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    GetActivity.create();
                                                    for (int i2 = 0; i2 < list.size(); i2++) {
                                                        if (activity_id==list.get(i2).getActivity_id()){
                                                            GetActivity.activityEntity.setActivity_theme(list.get(i2).getActivity_theme());
                                                            GetActivity.activityEntity.setActivity_id(list.get(i2).getActivity_id());
                                                            GetActivity.activityEntity.setActivity_manager(list.get(i2).getActivity_manager());
                                                            GetActivity.activityEntity.setActivity_department(list.get(i2).getActivity_department());
                                                            GetActivity.activityEntity.setActivity_participation(list.get(i2).getActivity_participation());
                                                            GetActivity.activityEntity.setActivity_time(list.get(i2).getActivity_time());
                                                            GetActivity.activityEntity.setActivity_address(list.get(i2).getActivity_address());
                                                            GetActivity.activityEntity.setUpdate_time(list.get(i2).getUpdate_time());
                                                            GetActivity.activityEntity.setCurrent_update_user_name(list.get(i2).getCurrent_update_user_name());
                                                            GetActivity.activityEntity.setCreate_time(list.get(i2).getCreate_time());
                                                            GetActivity.activityEntity.setCurrent_create_user_name(list.get(i2).getCurrent_create_user_name());
                                                            GetActivity.activityEntity.setActivity_description(list.get(i2).getActivity_description());
                                                            GetActivity.activityEntity.setPriority(list.get(i2).getPriority());
                                                            GetActivity.activityEntity.setPriority_id(list.get(i2).getPriority_id());
                                                            GetActivity.activityEntity.setTask_state_id(list.get(i2).getTask_state_id());
                                                            GetActivity.activityEntity.setTask_state(list.get(i2).getTask_state());
                                                        }

                                                    }

                                                    if (GetActivity.activityEntity != null) {
                                                        Intent intent2 = new Intent(DetailedHealthEducationActivity.this, detailedActivity.class);
                                                        startActivity(intent2);
                                                    }

                                                }
                                            });
                                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {

                                                }
                                            });
                                            builder.show();
                                        }
                                    }
                                });
                            }
                        }).start();
                        break;
                }
                return false;
            }
        });
        img_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(DetailedHealthEducationActivity.this,healthEducationAddActivity.class);
                intent.putExtra("tag","update");
                startActivity(intent);
            }
        });
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        dockinglist = new ArrayList<dockingEntity>();
                        dockDao dockDao=new dockDaoImpl();
                        dockinglist = dockDao.queryAllDockingMessage(baseClass.query_docking_message_url, null);
                        GetDockData.create();
                        GetDockData.dockingEntityList = dockinglist;
                        if (dockinglist.size() != 0) {
                            finish();
                        }
                    }
                }).start();


            }
        });
    }
    public void OnClick(View view) {
        switch (view.getId()){
            case R.id.detailed_dock_frame_other:
                ButtonTag2=false;
                ButtonTag1 = true;
                ButtonTag4 = false;
                btn_other.setBackgroundColor(Color.parseColor("#FFFFFF"));
                btn_base.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_main.setBackgroundColor(Color.parseColor("#E1E8ED"));
                manager.beginTransaction().replace(R.id.detailed_dock_frame_conner,fragment_other).commit();
                break;
            case R.id.detailed_dock_frame_basemess:
                ButtonTag1 = false;
                ButtonTag2=true;
                ButtonTag4 = false;
                btn_other.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_base.setBackgroundColor(Color.parseColor("#FFFFFF"));
                btn_main.setBackgroundColor(Color.parseColor("#E1E8ED"));
                manager.beginTransaction().replace(R.id.detailed_dock_frame_conner,fragment_base).commit();
                break;
            case R.id.detailed_dock_frame_main:
                ButtonTag1 = false;
                ButtonTag2=false;
                ButtonTag4 = true;
                btn_other.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_base.setBackgroundColor(Color.parseColor("#E1E8ED"));
                btn_main.setBackgroundColor(Color.parseColor("#FFFFFF"));
                manager.beginTransaction().replace(R.id.detailed_dock_frame_conner,fragment_main).commit();
                break;
            case R.id.detailed_dock_img_more:
                popupMenu.show();
                break;

        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        docking=null;
        docking=GetDock.get();
        txt_title.setText(docking.getDocking_name());
        if(ButtonTag2){
            fragment_base = new baseFragment();
            manager.beginTransaction().replace(R.id.detailed_dock_frame_conner,fragment_base).commitAllowingStateLoss();
        }else if (ButtonTag1){
            fragment_other = new otherFragment();
            manager.beginTransaction().replace(R.id.detailed_dock_frame_conner,fragment_other).commitAllowingStateLoss();
        }else if (ButtonTag4){
            fragment_main = new mainFragment();
            manager.beginTransaction().replace(R.id.detailed_dock_frame_conner,fragment_main).commitAllowingStateLoss();
        }

    }
}
